<?php

/**
 * @copyright (c) 2011
 */
App::uses('AppModel', 'Model');

class AcademicSession extends AppModel {

    var $name = 'AcademicSession';

    public $hasMany = array(
        'Transaction' => array(
            'className' => 'Transaction',
            'foreignKey' => 'academic_session_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'ServiceCharge' => array(
            'className' => 'ServiceCharge',
            'foreignKey' => 'academic_session_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'StudentServiceRequest' => array(
            'className' => 'StudentServiceRequest',
            'foreignKey' => 'academic_session_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public function save($data = null,$validate = true, $fieldList = array()) {
        $s = $this->sanitize($data);
        return parent::save($s);
    }

    function getAllSessions() {
        # fetch the specific data from the server and retrun it.
        return $this->find('all', array('order'=>array('id'=>'desc'),'recursive' => -1));
    }

    function getAcademicSessionById($id) {
        # fetch the specific data from the server and retrun it.
        $conditions = array(
            'id'=>$id
        );
        return $this->find('first', array('conditions'=>$conditions,'order'=>array('id'=>'desc'),'recursive' => -1));
    }

    function getAcademicYears() {
        # fetch the specific data from the server and retrun it.
        $data =  $this->find('all', array('order'=>array('id'=>'desc'),'recursive' => -1));
        $acc_years = array();
        foreach($data as $k => $val){
            $acc_years[$val['AcademicSession']['acc_year']] = $val['AcademicSession']['acc_year'];
        }

        return $acc_years;
    }
}